package com.baizhi.controller;

import com.baizhi.pojos.User;
import com.baizhi.service.LoginService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 访问登录页面
     * @return
     */
    @GetMapping("/login")
    public String tologin(){

        return "login";
    }

    /**
     * 登录
     */
    @PostMapping("/tologin")
    public String login(User user, HttpSession session){

        int i = loginService.login(user);

        if (i==1){

            QueryWrapper<User> wrapper = new QueryWrapper<>();

            wrapper.lambda().eq(User::getUname,user.getUname());

            User one = loginService.getOne(wrapper);

            session.setAttribute("user",one);

            return "redirect:/shop/shopList";
        }else {

            return "login";
        }
    }
}
